home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 008a / perl40_2.zip / PERL.C < prev    next >
C/C++ Source or Header  |  1991-12-25  |  37KB  |  1,458 lines

  1. char rcsid[] = "$RCSfile: perl.c,v $$Revision: 4.0.1.6 $$Date: 91/11/11 16:38:45 $\nPatch level: ###\n";
  2. /*
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    perl.c,v $
  9.  * Revision 4.0.1.6  91/11/11  16:38:45  lwall
  10.  * patch19: default arg for shift was wrong after first subroutine definition
  11.  * patch19: op/regexp.t failed from missing arg to bcmp()
  12.  *
  13.  * Revision 4.0.1.5  91/11/05  18:03:32  lwall
  14.  * patch11: random cleanup
  15.  * patch11: $0 was being truncated at times
  16.  * patch11: cppstdin now installed outside of source directory
  17.  * patch11: -P didn't allow use of #elif or #undef
  18.  * patch11: prepared for ctype implementations that don't define isascii()
  19.  * patch11: added eval {}
  20.  * patch11: eval confused by string containing null
  21.  *
  22.  * Revision 4.0.1.4  91/06/10  01:23:07  lwall
  23.  * patch10: perl -v printed incorrect copyright notice
  24.  *
  25.  * Revision 4.0.1.3  91/06/07  11:40:18  lwall
  26.  * patch4: changed old $^P to $^X
  27.  *
  28.  * Revision 4.0.1.2  91/06/07  11:26:16  lwall
  29.  * patch4: new copyright notice
  30.  * patch4: added $^P variable to control calling of perldb routines
  31.  * patch4: added $^F variable to specify maximum system fd, default 2
  32.  * patch4: debugger lost track of lines in eval
  33.  *
  34.  * Revision 4.0.1.1  91/04/11  17:49:05  lwall
  35.  * patch1: fixed undefined environ problem
  36.  *
  37.  * Revision 4.0  91/03/20  01:37:44  lwall
  38.  * 4.0 baseline.
  39.  *
  40.  */
  41.  
  42.  
  43. /*SUPPRESS 560*/
  44.  
  45.  
  46. #include "EXTERN.h"
  47. #include "perl.h"
  48. #include "perly.h"
  49. #ifdef MSDOS
  50. #include "patchlev.h"
  51. #include <process.h>
  52. #include <dos.h>
  53. #else
  54. #include "patchlevel.h"
  55. #endif
  56.  
  57.  
  58. char *getenv();
  59.  
  60.  
  61. #ifdef IAMSUID
  62. #ifndef DOSUID
  63. #define DOSUID
  64. #endif
  65. #endif
  66.  
  67.  
  68. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  69. #ifdef DOSUID
  70. #undef DOSUID
  71. #endif
  72. #endif
  73.  
  74.  
  75. static char* moreswitches();
  76. static char* cddir;
  77. static bool minus_c;
  78. static char patchlevel[6];
  79. static char *nrs = "\n";
  80. static int nrschar = '\n';      /* final char of rs, or 0777 if none */
  81. static int nrslen = 1;
  82.  
  83.  
  84. main(argc,argv,env)
  85. register int argc;
  86. register char **argv;
  87. register char **env;
  88. {
  89.     register STR *str;
  90.     register char *s;
  91.     char *scriptname;
  92.     char *getenv();
  93.     bool dosearch = FALSE;
  94. #ifdef DOSUID
  95.     char *validarg = "";
  96. #endif
  97.  
  98.  
  99. #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
  100. #ifdef IAMSUID
  101. #undef IAMSUID
  102.     fatal("suidperl is no longer needed since the kernel can now execute\n\
  103. setuid perl scripts securely.\n");
  104. #endif
  105. #endif
  106.  
  107.  
  108.     origargv = argv;
  109.     origargc = argc;
  110.     origenviron = environ;
  111.     uid = (int)getuid();
  112.     euid = (int)geteuid();
  113.     gid = (int)getgid();
  114.     egid = (int)getegid();
  115.     sprintf(patchlevel,"%3.3s%2.2d", index(rcsid,'4'), PATCHLEVEL);
  116. #ifdef MSDOS
  117.     /*
  118.      * There is no way we can refer to them from Perl so close them to save
  119.      * space.  The other alternative would be to provide STDAUX and STDPRN
  120.      * filehandles.
  121.      */
  122.     (void)fclose(stdaux);
  123.     (void)fclose(stdprn);
  124. #ifdef __OVERLAY__
  125.     _OvrInitEms(0,0,32);
  126. #endif
  127. #endif
  128.     if (do_undump) {
  129.     origfilename = savestr(argv[0]);
  130.     do_undump = 0;
  131.     loop_ptr = -1;        /* start label stack again */
  132.     goto just_doit;
  133.     }
  134.     (void)sprintf(index(rcsid,'#'), "%d\n", PATCHLEVEL);
  135.     linestr = Str_new(65,80);
  136.     str_nset(linestr,"",0);
  137.     str = str_make("",0);        /* first used for -I flags */
  138.     curstash = defstash = hnew(0);
  139.     curstname = str_make("main",4);
  140.     stab_xhash(stabent("_main",TRUE)) = defstash;
  141.     defstash->tbl_name = "main";
  142.     incstab = hadd(aadd(stabent("INC",TRUE)));
  143.     incstab->str_pok |= SP_MULTI;
  144.     for (argc--,argv++; argc > 0; argc--,argv++) {
  145.     if (argv[0][0] != '-' || !argv[0][1])
  146.         break;
  147. #ifdef DOSUID
  148.     if (*validarg)
  149.     validarg = " PHOOEY ";
  150.     else
  151.     validarg = argv[0];
  152. #endif
  153.     s = argv[0]+1;
  154.       reswitch:
  155.     switch (*s) {
  156.     case '0':
  157.     case 'a':
  158.     case 'c':
  159.     case 'd':
  160.     case 'D':
  161.     case 'i':
  162.     case 'l':
  163.     case 'n':
  164.     case 'p':
  165.     case 'u':
  166.     case 'U':
  167.     case 'v':
  168.     case 'w':
  169.         if (s = moreswitches(s))
  170.         goto reswitch;
  171.         break;
  172.  
  173.  
  174.     case 'e':
  175. #ifdef TAINT
  176.         if (euid != uid || egid != gid)
  177.         fatal("No -e allowed in setuid scripts");
  178. #endif
  179.         if (!e_fp) {
  180.             e_tmpname = savestr(TMPPATH);
  181.         (void)mktemp(e_tmpname);
  182.         e_fp = fopen(e_tmpname,"w");
  183.         if (!e_fp)
  184.             fatal("Cannot open temporary file");
  185.         }
  186.         if (argv[1]) {
  187.         fputs(argv[1],e_fp);
  188.         argc--,argv++;
  189.         }
  190.         (void)putc('\n', e_fp);
  191.         break;
  192.     case 'I':
  193. #ifdef TAINT
  194.         if (euid != uid || egid != gid)
  195.         fatal("No -I allowed in setuid scripts");
  196. #endif
  197.         str_cat(str,"-");
  198.         str_cat(str,s);
  199.         str_cat(str," ");
  200.         if (*++s) {
  201.         (void)apush(stab_array(incstab),str_make(s,0));
  202.         }
  203.         else if (argv[1]) {
  204.         (void)apush(stab_array(incstab),str_make(argv[1],0));
  205.         str_cat(str,argv[1]);
  206.         argc--,argv++;
  207.         str_cat(str," ");
  208.         }
  209.         break;
  210.     case 'P':
  211. #ifdef TAINT
  212.         if (euid != uid || egid != gid)
  213.         fatal("No -P allowed in setuid scripts");
  214. #endif
  215.         preprocess = TRUE;
  216.         s++;
  217.         goto reswitch;
  218.     case 's':
  219. #ifdef TAINT
  220.         if (euid != uid || egid != gid)
  221.         fatal("No -s allowed in setuid scripts");
  222. #endif
  223.         doswitches = TRUE;
  224.         s++;
  225.         goto reswitch;
  226.     case 'S':
  227. #ifdef TAINT
  228.         if (euid != uid || egid != gid)
  229.         fatal("No -S allowed in setuid scripts");
  230. #endif
  231.         dosearch = TRUE;
  232.         s++;
  233.         goto reswitch;
  234.     case 'x':
  235.         doextract = TRUE;
  236.         s++;
  237.         if (*s)
  238.         cddir = savestr(s);
  239.         break;
  240.     case '-':
  241.         argc--,argv++;
  242.         goto switch_end;
  243.     case 0:
  244.         break;
  245.     default:
  246.         fatal("Unrecognized switch: -%s",s);
  247.     }
  248.     }
  249.   switch_end:
  250.     scriptname = argv[0];
  251.     if (e_fp) {
  252.     (void)fclose(e_fp);
  253.     argc++,argv--;
  254.     scriptname = e_tmpname;
  255.     }
  256.  
  257.  
  258. #ifdef MSDOS
  259. #define PERLLIB_SEP ';'
  260. #else
  261. #define PERLLIB_SEP ':'
  262. #endif
  263. #ifndef TAINT        /* Can't allow arbitrary PERLLIB in setuid script */
  264.     {
  265.     char * s2 = getenv("PERLLIB");
  266.  
  267.  
  268.     if ( s2 ) {
  269.         /* Break at all separators */
  270.         while ( *s2 ) {
  271.         /* First, skip any consecutive separators */
  272.         while ( *s2 == PERLLIB_SEP ) {
  273.             /* Uncomment the next line for PATH semantics */
  274.             /* (void)apush(stab_array(incstab),str_make(".",1)); */
  275.             s2++;
  276.         }
  277.         if ( (s = index(s2,PERLLIB_SEP)) != Nullch ) {
  278.             (void)apush(stab_array(incstab),str_make(s2,(int)(s-s2)));
  279.             s2 = s+1;
  280.         } else {
  281.             (void)apush(stab_array(incstab),str_make(s2,0));
  282.             break;
  283.         }
  284.         }
  285.     }
  286.     }
  287. #endif /* TAINT */
  288.  
  289.  
  290. #ifndef PRIVLIB
  291. #define PRIVLIB "/usr/local/lib/perl"
  292. #endif
  293.     (void)apush(stab_array(incstab),str_make(PRIVLIB,0));
  294.     (void)apush(stab_array(incstab),str_make(".",1));
  295.  
  296.  
  297.     str_set(&str_no,No);
  298.     str_set(&str_yes,Yes);
  299.  
  300.  
  301.     /* open script */
  302.  
  303.  
  304.     if (scriptname == Nullch)
  305. #ifdef MSDOS
  306.     {
  307.     if ( isatty(fileno(stdin)) )
  308.       moreswitches("v");
  309.     scriptname = "-";
  310.     }
  311. #else
  312.     scriptname = "-";
  313. #endif
  314.     if (dosearch && !index(scriptname, '/') && (s = getenv("PATH"))) {
  315.     char *xfound = Nullch, *xfailed = Nullch;
  316.     int len;
  317.  
  318.  
  319.     bufend = s + strlen(s);
  320.     while (*s) {
  321. #ifndef MSDOS
  322.         s = cpytill(tokenbuf,s,bufend,':',&len);
  323. #else
  324.         for (len = 0; *s && *s != ';'; tokenbuf[len++] = *s++);
  325.         tokenbuf[len] = '\0';
  326. #endif
  327.         if (*s)
  328.         s++;
  329. #ifndef MSDOS
  330.         if (len && tokenbuf[len-1] != '/')
  331. #else
  332.         if (len && tokenbuf[len-1] != '\\')
  333. #endif
  334.         (void)strcat(tokenbuf+len,"/");
  335.         (void)strcat(tokenbuf+len,scriptname);
  336. #ifdef DEBUGGING
  337.         if (debug & 1)
  338.         fprintf(stderr,"Looking for %s\n",tokenbuf);
  339. #endif
  340.         if (stat(tokenbuf,&statbuf) < 0)        /* not there? */
  341.         continue;
  342.         if (S_ISREG(statbuf.st_mode)
  343.          && cando(S_IRUSR,TRUE,&statbuf) && cando(S_IXUSR,TRUE,&statbuf)) {
  344.         xfound = tokenbuf;              /* bingo! */
  345.         break;
  346.         }
  347.         if (!xfailed)
  348.         xfailed = savestr(tokenbuf);
  349.     }
  350.     if (!xfound)
  351.         fatal("Can't execute %s", xfailed ? xfailed : scriptname );
  352.     if (xfailed)
  353.         Safefree(xfailed);
  354.     scriptname = savestr(xfound);
  355.     }
  356.  
  357.  
  358.     fdpid = anew(Nullstab);    /* for remembering popen pids by fd */
  359.     pidstatus = hnew(COEFFSIZE);/* for remembering status of dead pids */
  360.  
  361.  
  362.     origfilename = savestr(scriptname);
  363.     curcmd->c_filestab = fstab(origfilename);
  364.     if (strEQ(origfilename,"-"))
  365.     scriptname = "";
  366.     if (preprocess) {
  367.     char *cpp = CPPSTDIN;
  368.  
  369.  
  370.     if (strEQ(cpp,"cppstdin"))
  371.         sprintf(tokenbuf, "%s/%s", SCRIPTDIR, cpp);
  372.     else
  373.         sprintf(tokenbuf, "%s", cpp);
  374.     str_cat(str,"-I");
  375.     str_cat(str,PRIVLIB);
  376.     (void)sprintf(buf, "\
  377. %ssed %s -e '/^[^#]/b' \
  378.  -e '/^#[     ]*include[     ]/b' \
  379.  -e '/^#[     ]*define[     ]/b' \
  380.  -e '/^#[     ]*if[     ]/b' \
  381.  -e '/^#[     ]*ifdef[     ]/b' \
  382.  -e '/^#[     ]*ifndef[     ]/b' \
  383.  -e '/^#[     ]*else/b' \
  384.  -e '/^#[     ]*elif[     ]/b' \
  385.  -e '/^#[     ]*undef[     ]/b' \
  386.  -e '/^#[     ]*endif/b' \
  387.  -e 's/^[     ]*#.*//' \
  388.  %s | %s -C %s %s",
  389. #ifdef MSDOS
  390.       "",
  391. #else
  392.       "/bin/",
  393. #endif
  394.       (doextract ? "-e '1,/^#/d\n'" : ""),
  395.       scriptname, tokenbuf, str_get(str), CPPMINUS);
  396. #ifdef DEBUGGING
  397.     if (debug & 64) {
  398.         fputs(buf,stderr);
  399.         fputs("\n",stderr);
  400.     }
  401. #endif
  402.     doextract = FALSE;
  403. #ifdef IAMSUID                /* actually, this is caught earlier */
  404.     if (euid != uid && !euid)    /* if running suidperl */
  405. #ifdef HAS_SETEUID
  406.         (void)seteuid(uid);        /* musn't stay setuid root */
  407. #else
  408. #ifdef HAS_SETREUID
  409.         (void)setreuid(-1, uid);
  410. #else
  411.         setuid(uid);
  412. #endif
  413. #endif
  414. #endif /* IAMSUID */
  415.     rsfp = mypopen(buf,"r");
  416.     }
  417.     else if (!*scriptname) {
  418. #ifdef TAINT
  419.     if (euid != uid || egid != gid)
  420.         fatal("Can't take set-id script from stdin");
  421. #endif
  422.     rsfp = stdin;
  423.     }
  424.     else
  425.     rsfp = fopen(scriptname,"r");
  426.     if ((FILE*)rsfp == Nullfp) {
  427. #ifdef DOSUID
  428. #ifndef IAMSUID        /* in case script is not readable before setuid */
  429.     if (euid && stat(stab_val(curcmd->c_filestab)->str_ptr,&statbuf) >= 0 &&
  430.       statbuf.st_mode & (S_ISUID|S_ISGID)) {
  431.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  432.         execv(buf, origargv);    /* try again */
  433.         fatal("Can't do setuid\n");
  434.     }
  435. #endif
  436. #endif
  437.     fatal("Can't open perl script \"%s\": %s\n",
  438.       stab_val(curcmd->c_filestab)->str_ptr, strerror(errno));
  439.     }
  440.     str_free(str);        /* free -I directories */
  441.     str = Nullstr;
  442.  
  443.  
  444.     /* do we need to emulate setuid on scripts? */
  445.  
  446.  
  447.     /* This code is for those BSD systems that have setuid #! scripts disabled
  448.      * in the kernel because of a security problem.  Merely defining DOSUID
  449.      * in perl will not fix that problem, but if you have disabled setuid
  450.      * scripts in the kernel, this will attempt to emulate setuid and setgid
  451.      * on scripts that have those now-otherwise-useless bits set.  The setuid
  452.      * root version must be called suidperl or sperlN.NNN.  If regular perl
  453.      * discovers that it has opened a setuid script, it calls suidperl with
  454.      * the same argv that it had.  If suidperl finds that the script it has
  455.      * just opened is NOT setuid root, it sets the effective uid back to the
  456.      * uid.  We don't just make perl setuid root because that loses the
  457.      * effective uid we had before invoking perl, if it was different from the
  458.      * uid.
  459.      *
  460.      * DOSUID must be defined in both perl and suidperl, and IAMSUID must
  461.      * be defined in suidperl only.  suidperl must be setuid root.  The
  462.      * Configure script will set this up for you if you want it.
  463.      *
  464.      * There is also the possibility of have a script which is running
  465.      * set-id due to a C wrapper.  We want to do the TAINT checks
  466.      * on these set-id scripts, but don't want to have the overhead of
  467.      * them in normal perl, and can't use suidperl because it will lose
  468.      * the effective uid info, so we have an additional non-setuid root
  469.      * version called taintperl or tperlN.NNN that just does the TAINT checks.
  470.      */
  471.  
  472.  
  473. #ifdef DOSUID
  474.     if (fstat(fileno(rsfp),&statbuf) < 0)    /* normal stat is insecure */
  475.     fatal("Can't stat script \"%s\"",origfilename);
  476.     if (statbuf.st_mode & (S_ISUID|S_ISGID)) {
  477.     int len;
  478.  
  479.  
  480. #ifdef IAMSUID
  481. #ifndef HAS_SETREUID
  482.     /* On this access check to make sure the directories are readable,
  483.      * there is actually a small window that the user could use to make
  484.      * filename point to an accessible directory.  So there is a faint
  485.      * chance that someone could execute a setuid script down in a
  486.      * non-accessible directory.  I don't know what to do about that.
  487.      * But I don't think it's too important.  The manual lies when
  488.      * it says access() is useful in setuid programs.
  489.      */
  490.     if (access(stab_val(curcmd->c_filestab)->str_ptr,1))    /*double check*/
  491.         fatal("Permission denied");
  492. #else
  493.     /* If we can swap euid and uid, then we can determine access rights
  494.      * with a simple stat of the file, and then compare device and
  495.      * inode to make sure we did stat() on the same file we opened.
  496.      * Then we just have to make sure he or she can execute it.
  497.      */
  498.     {
  499.         struct stat tmpstatbuf;
  500.  
  501.  
  502.         if (setreuid(euid,uid) < 0 || getuid() != euid || geteuid() != uid)
  503.         fatal("Can't swap uid and euid");    /* really paranoid */
  504.         if (stat(stab_val(curcmd->c_filestab)->str_ptr,&tmpstatbuf) < 0)
  505.         fatal("Permission denied");    /* testing full pathname here */
  506.         if (tmpstatbuf.st_dev != statbuf.st_dev ||
  507.         tmpstatbuf.st_ino != statbuf.st_ino) {
  508.         (void)fclose(rsfp);
  509.         if (rsfp = mypopen("/bin/mail root","w")) {    /* heh, heh */
  510.             fprintf(rsfp,
  511. "User %d tried to run dev %d ino %d in place of dev %d ino %d!\n\
  512. (Filename of set-id script was %s, uid %d gid %d.)\n\nSincerely,\nperl\n",
  513.             uid,tmpstatbuf.st_dev, tmpstatbuf.st_ino,
  514.             statbuf.st_dev, statbuf.st_ino,
  515.             stab_val(curcmd->c_filestab)->str_ptr,
  516.             statbuf.st_uid, statbuf.st_gid);
  517.             (void)mypclose(rsfp);
  518.         }
  519.         fatal("Permission denied\n");
  520.         }
  521.         if (setreuid(uid,euid) < 0 || getuid() != uid || geteuid() != euid)
  522.         fatal("Can't reswap uid and euid");
  523.         if (!cando(S_IXUSR,FALSE,&statbuf))        /* can real uid exec? */
  524.         fatal("Permission denied\n");
  525.     }
  526. #endif /* HAS_SETREUID */
  527. #endif /* IAMSUID */
  528.  
  529.  
  530.     if (!S_ISREG(statbuf.st_mode))
  531.         fatal("Permission denied");
  532.     if (statbuf.st_mode & S_IWOTH)
  533.         fatal("Setuid/gid script is writable by world");
  534.     doswitches = FALSE;        /* -s is insecure in suid */
  535.     curcmd->c_line++;
  536.     if (fgets(tokenbuf,sizeof tokenbuf, rsfp) == Nullch ||
  537.       strnNE(tokenbuf,"#!",2) )    /* required even on Sys V */
  538.         fatal("No #! line");
  539.     s = tokenbuf+2;
  540.     if (*s == ' ') s++;
  541.     while (!isSPACE(*s)) s++;
  542.     if (strnNE(s-4,"perl",4) && strnNE(s-9,"perl",4))  /* sanity check */
  543.         fatal("Not a perl script");
  544.     while (*s == ' ' || *s == '\t') s++;
  545.     /*
  546.      * #! arg must be what we saw above.  They can invoke it by
  547.      * mentioning suidperl explicitly, but they may not add any strange
  548.      * arguments beyond what #! says if they do invoke suidperl that way.
  549.      */
  550.     len = strlen(validarg);
  551.     if (strEQ(validarg," PHOOEY ") ||
  552.         strnNE(s,validarg,len) || !isSPACE(s[len]))
  553.         fatal("Args must match #! line");
  554.  
  555.  
  556. #ifndef IAMSUID
  557.     if (euid != uid && (statbuf.st_mode & S_ISUID) &&
  558.         euid == statbuf.st_uid)
  559.         if (!do_undump)
  560.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  561. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  562. #endif /* IAMSUID */
  563.  
  564.  
  565.     if (euid) {    /* oops, we're not the setuid root perl */
  566.         (void)fclose(rsfp);
  567. #ifndef IAMSUID
  568.         (void)sprintf(buf, "%s/sperl%s", BIN, patchlevel);
  569.         execv(buf, origargv);    /* try again */
  570. #endif
  571.         fatal("Can't do setuid\n");
  572.     }
  573.  
  574.  
  575.     if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid)
  576. #ifdef HAS_SETEGID
  577.         (void)setegid(statbuf.st_gid);
  578. #else
  579. #ifdef HAS_SETREGID
  580.         (void)setregid((GIDTYPE)-1,statbuf.st_gid);
  581. #else
  582.         setgid(statbuf.st_gid);
  583. #endif
  584. #endif
  585.     if (statbuf.st_mode & S_ISUID) {
  586.         if (statbuf.st_uid != euid)
  587. #ifdef HAS_SETEUID
  588.         (void)seteuid(statbuf.st_uid);    /* all that for this */
  589. #else
  590. #ifdef HAS_SETREUID
  591.         (void)setreuid((UIDTYPE)-1,statbuf.st_uid);
  592. #else
  593.         setuid(statbuf.st_uid);
  594. #endif
  595. #endif
  596.     }
  597.     else if (uid)            /* oops, mustn't run as root */
  598. #ifdef HAS_SETEUID
  599.         (void)seteuid((UIDTYPE)uid);
  600. #else
  601. #ifdef HAS_SETREUID
  602.         (void)setreuid((UIDTYPE)-1,(UIDTYPE)uid);
  603. #else
  604.         setuid((UIDTYPE)uid);
  605. #endif
  606. #endif
  607.     uid = (int)getuid();
  608.     euid = (int)geteuid();
  609.     gid = (int)getgid();
  610.     egid = (int)getegid();
  611.     if (!cando(S_IXUSR,TRUE,&statbuf))
  612.         fatal("Permission denied\n");    /* they can't do this */
  613.     }
  614. #ifdef IAMSUID
  615.     else if (preprocess)
  616.     fatal("-P not allowed for setuid/setgid script\n");
  617.     else
  618.     fatal("Script is not setuid/setgid in suidperl\n");
  619. #else
  620. #ifndef TAINT        /* we aren't taintperl or suidperl */
  621.     /* script has a wrapper--can't run suidperl or we lose euid */
  622.     else if (euid != uid || egid != gid) {
  623.     (void)fclose(rsfp);
  624.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  625.     execv(buf, origargv);    /* try again */
  626.     fatal("Can't run setuid script with taint checks");
  627.     }
  628. #endif /* TAINT */
  629. #endif /* IAMSUID */
  630. #else /* !DOSUID */
  631. #ifndef TAINT        /* we aren't taintperl or suidperl */
  632.     if (euid != uid || egid != gid) {    /* (suidperl doesn't exist, in fact) */
  633. #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
  634.     fstat(fileno(rsfp),&statbuf);    /* may be either wrapped or real suid */
  635.     if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
  636.         ||
  637.         (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
  638.        )
  639.         if (!do_undump)
  640.         fatal("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
  641. FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
  642. #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
  643.     /* not set-id, must be wrapped */
  644.     (void)fclose(rsfp);
  645.     (void)sprintf(buf, "%s/tperl%s", BIN, patchlevel);
  646.     execv(buf, origargv);    /* try again */
  647.     fatal("Can't run setuid script with taint checks");
  648.     }
  649. #endif /* TAINT */
  650. #endif /* DOSUID */
  651.  
  652.  
  653. #if !defined(IAMSUID) && !defined(TAINT)
  654.  
  655.  
  656.     /* skip forward in input to the real script? */
  657.  
  658.  
  659.     while (doextract) {
  660.     if ((s = str_gets(linestr, rsfp, 0)) == Nullch)
  661.         fatal("No Perl script found in input\n");
  662.     if (*s == '#' && s[1] == '!' && instr(s,"perl")) {
  663.         ungetc('\n',rsfp);        /* to keep line count right */
  664.         doextract = FALSE;
  665.         if (s = instr(s,"perl -")) {
  666.         s += 6;
  667.         /*SUPPRESS 530*/
  668.         while (s = moreswitches(s)) ;
  669.         }
  670.         if (cddir && chdir(cddir) < 0)
  671.         fatal("Can't chdir to %s",cddir);
  672.     }
  673.     }
  674. #endif /* !defined(IAMSUID) && !defined(TAINT) */
  675.  
  676.  
  677.     defstab = stabent("_",TRUE);
  678.  
  679.  
  680.     subname = str_make("main",4);
  681.     if (perldb) {
  682.     debstash = hnew(0);
  683.     stab_xhash(stabent("_DB",TRUE)) = debstash;
  684.     curstash = debstash;
  685.     dbargs = stab_xarray(aadd((tmpstab = stabent("args",TRUE))));
  686.     tmpstab->str_pok |= SP_MULTI;
  687.     dbargs->ary_flags = 0;
  688.     DBstab = stabent("DB",TRUE);
  689.     DBstab->str_pok |= SP_MULTI;
  690.     DBline = stabent("dbline",TRUE);
  691.     DBline->str_pok |= SP_MULTI;
  692.     DBsub = hadd(tmpstab = stabent("sub",TRUE));
  693.     tmpstab->str_pok |= SP_MULTI;
  694.     DBsingle = stab_val((tmpstab = stabent("single",TRUE)));
  695.     tmpstab->str_pok |= SP_MULTI;
  696.     DBtrace = stab_val((tmpstab = stabent("trace",TRUE)));
  697.     tmpstab->str_pok |= SP_MULTI;
  698.     DBsignal = stab_val((tmpstab = stabent("signal",TRUE)));
  699.     tmpstab->str_pok |= SP_MULTI;
  700.     curstash = defstash;
  701.     }
  702.  
  703.  
  704.     /* init tokener */
  705.  
  706.  
  707.     bufend = bufptr = str_get(linestr);
  708.  
  709.  
  710.     savestack = anew(Nullstab);        /* for saving non-local values */
  711.     stack = anew(Nullstab);        /* for saving non-local values */
  712.     stack->ary_flags = 0;        /* not a real array */
  713.     afill(stack,63); afill(stack,-1);    /* preextend stack */
  714.     afill(savestack,63); afill(savestack,-1);
  715.  
  716.  
  717.     /* now parse the script */
  718.  
  719.  
  720.     error_count = 0;
  721.     if (yyparse() || error_count) {
  722.     if (minus_c)
  723.         fatal("%s had compilation errors.\n", origfilename);
  724.     else {
  725.         fatal("Execution of %s aborted due to compilation errors.\n",
  726.         origfilename);
  727.     }
  728.     }
  729.  
  730.  
  731.     New(50,loop_stack,128,struct loop);
  732. #ifdef DEBUGGING
  733.     if (debug) {
  734.     New(51,debname,128,char);
  735.     New(52,debdelim,128,char);
  736.     }
  737. #endif
  738.     curstash = defstash;
  739.  
  740.  
  741.     preprocess = FALSE;
  742.     if (e_fp) {
  743.     e_fp = Nullfp;
  744.     (void)UNLINK(e_tmpname);
  745.     }
  746.  
  747.  
  748.     /* initialize everything that won't change if we undump */
  749.  
  750.  
  751.     if (sigstab = stabent("SIG",allstabs)) {
  752.     sigstab->str_pok |= SP_MULTI;
  753.     (void)hadd(sigstab);
  754.     }
  755.  
  756.  
  757.     magicalize("!#?^~=-%.+&*()<>,\\/[|`':\004\t\020\024\027\006");
  758.     userinit();        /* in case linked C routines want magical variables */
  759.  
  760.  
  761.     amperstab = stabent("&",allstabs);
  762.     leftstab = stabent("`",allstabs);
  763.     rightstab = stabent("'",allstabs);
  764.     sawampersand = (amperstab || leftstab || rightstab);
  765.     if (tmpstab = stabent(":",allstabs))
  766.     str_set(STAB_STR(tmpstab),chopset);
  767.     if (tmpstab = stabent("\024",allstabs))
  768.     time(&basetime);
  769.  
  770.  
  771.     /* these aren't necessarily magical */
  772.     if (tmpstab = stabent(";",allstabs))
  773.     str_set(STAB_STR(tmpstab),"\034");
  774.     if (tmpstab = stabent("]",allstabs)) {
  775.     str = STAB_STR(tmpstab);
  776.     str_set(str,rcsid);
  777.     str->str_u.str_nval = atof(patchlevel);
  778.     str->str_nok = 1;
  779.     }
  780.     str_nset(stab_val(stabent("\"", TRUE)), " ", 1);
  781.  
  782.  
  783.     stdinstab = stabent("STDIN",TRUE);
  784.     stdinstab->str_pok |= SP_MULTI;
  785.     stab_io(stdinstab) = stio_new();
  786.     stab_io(stdinstab)->ifp = stdin;
  787.     tmpstab = stabent("stdin",TRUE);
  788.     stab_io(tmpstab) = stab_io(stdinstab);
  789.     tmpstab->str_pok |= SP_MULTI;
  790.  
  791.  
  792.     tmpstab = stabent("STDOUT",TRUE);
  793.     tmpstab->str_pok |= SP_MULTI;
  794.     stab_io(tmpstab) = stio_new();
  795.     stab_io(tmpstab)->ofp = stab_io(tmpstab)->ifp = stdout;
  796.     defoutstab = tmpstab;
  797.     tmpstab = stabent("stdout",TRUE);
  798.     stab_io(tmpstab) = stab_io(defoutstab);
  799.     tmpstab->str_pok |= SP_MULTI;
  800.  
  801.  
  802.     curoutstab = stabent("STDERR",TRUE);
  803.     curoutstab->str_pok |= SP_MULTI;
  804.     stab_io(curoutstab) = stio_new();
  805.     stab_io(curoutstab)->ofp = stab_io(curoutstab)->ifp = stderr;
  806.     tmpstab = stabent("stderr",TRUE);
  807.     stab_io(tmpstab) = stab_io(curoutstab);
  808.     tmpstab->str_pok |= SP_MULTI;
  809.     curoutstab = defoutstab;        /* switch back to STDOUT */
  810.  
  811.  
  812.     statname = Str_new(66,0);        /* last filename we did stat on */
  813.  
  814.  
  815.     /* now that script is parsed, we can modify record separator */
  816.  
  817.  
  818.     rs = nrs;
  819.     rslen = nrslen;
  820.     rschar = nrschar;
  821.     str_nset(stab_val(stabent("/", TRUE)), rs, rslen);
  822.  
  823.  
  824.     if (do_undump)
  825.     my_unexec();
  826.  
  827.  
  828.   just_doit:        /* come here if running an undumped a.out */
  829.     argc--,argv++;    /* skip name of script */
  830.     if (doswitches) {
  831.     for (; argc > 0 && **argv == '-'; argc--,argv++) {
  832.         if (argv[0][1] == '-') {
  833.         argc--,argv++;
  834.         break;
  835.         }
  836.         if (s = index(argv[0], '=')) {
  837.         *s++ = '\0';
  838.         str_set(stab_val(stabent(argv[0]+1,TRUE)),s);
  839.         }
  840.         else
  841.         str_numset(stab_val(stabent(argv[0]+1,TRUE)),(double)1.0);
  842.     }
  843.     }
  844. #ifdef TAINT
  845.     tainted = 1;
  846. #endif
  847.     if (tmpstab = stabent("0",allstabs)) {
  848.     str_set(stab_val(tmpstab),origfilename);
  849.     magicname("0", Nullch, 0);
  850.     }
  851.     if (tmpstab = stabent("\030",allstabs))
  852.     str_set(stab_val(tmpstab),origargv[0]);
  853.     if (argvstab = stabent("ARGV",allstabs)) {
  854.     argvstab->str_pok |= SP_MULTI;
  855.     (void)aadd(argvstab);
  856.     aclear(stab_array(argvstab));
  857.     for (; argc > 0; argc--,argv++) {
  858.         (void)apush(stab_array(argvstab),str_make(argv[0],0));
  859.     }
  860.     }
  861. #ifdef TAINT
  862.     (void) stabent("ENV",TRUE);        /* must test PATH and IFS */
  863. #endif
  864.     if (envstab = stabent("ENV",allstabs)) {
  865.     envstab->str_pok |= SP_MULTI;
  866.     (void)hadd(envstab);
  867.     hclear(stab_hash(envstab), FALSE);
  868.     if (env != environ)
  869.         environ[0] = Nullch;
  870.     for (; *env; env++) {
  871.         if (!(s = index(*env,'=')))
  872.         continue;
  873.         *s++ = '\0';
  874.         str = str_make(s--,0);
  875.         str_magic(str, envstab, 'E', *env, s - *env);
  876.         (void)hstore(stab_hash(envstab), *env, s - *env, str, 0);
  877.         *s = '=';
  878.     }
  879.     }
  880. #ifdef TAINT
  881.     tainted = 0;
  882. #endif
  883.     if (tmpstab = stabent("$",allstabs))
  884.     str_numset(STAB_STR(tmpstab),(double)getpid());
  885.  
  886.  
  887.     if (dowarn) {
  888.     stab_check('A','Z');
  889.     stab_check('a','z');
  890.     }
  891.  
  892.  
  893.     if (setjmp(top_env))    /* sets goto_targ on longjump */
  894.     loop_ptr = -1;        /* start label stack again */
  895.  
  896.  
  897. #ifdef DEBUGGING
  898.     if (debug & 1024)
  899.     dump_all();
  900.     if (debug)
  901.     fprintf(stderr,"\nEXECUTING...\n\n");
  902. #endif
  903.  
  904.  
  905.     if (minus_c) {
  906.     fprintf(stderr,"%s syntax OK\n", origfilename);
  907.     exit(0);
  908.     }
  909.  
  910.  
  911.     /* do it */
  912.  
  913.  
  914.     (void) cmd_exec(main_root,G_SCALAR,-1);
  915.  
  916.  
  917.     if (goto_targ)
  918.     fatal("Can't find label \"%s\"--aborting",goto_targ);
  919.     exit(0);
  920.     /* NOTREACHED */
  921. }
  922.  
  923.  
  924. void
  925. magicalize(list)
  926. register char *list;
  927. {
  928.     char sym[2];
  929.  
  930.  
  931.     sym[1] = '\0';
  932.     while (*sym = *list++)
  933.     magicname(sym, Nullch, 0);
  934. }
  935.  
  936.  
  937. void
  938. magicname(sym,name,namlen)
  939. char *sym;
  940. char *name;
  941. int namlen;
  942. {
  943.     register STAB *stab;
  944.  
  945.  
  946.     if (stab = stabent(sym,allstabs)) {
  947.     stab_flags(stab) = SF_VMAGIC;
  948.     str_magic(stab_val(stab), stab, 0, name, namlen);
  949.     }
  950. }
  951.  
  952.  
  953. void
  954. savelines(array, str)
  955. ARRAY *array;
  956. STR *str;
  957. {
  958.     register char *s = str->str_ptr;
  959.     register char *send = str->str_ptr + str->str_cur;
  960.     register char *t;
  961.     register int line = 1;
  962.  
  963.  
  964.     while (s && s < send) {
  965.     STR *tmpstr = Str_new(85,0);
  966.  
  967.  
  968.     t = index(s, '\n');
  969.     if (t)
  970.         t++;
  971.     else
  972.         t = send;
  973.  
  974.  
  975.     str_nset(tmpstr, s, t - s);
  976.     astore(array, line++, tmpstr);
  977.     s = t;
  978.     }
  979. }
  980.  
  981.  
  982. /* this routine is in perl.c by virtue of being sort of an alternate main() */
  983.  
  984.  
  985. int
  986. do_eval(str,optype,stash,savecmd,gimme,arglast)
  987. STR *str;
  988. int optype;
  989. HASH *stash;
  990. int savecmd;
  991. int gimme;
  992. int *arglast;
  993. {
  994.     STR **st = stack->ary_array;
  995.     int retval;
  996.     CMD *myroot = Nullcmd;
  997.     ARRAY *ar;
  998.     int i;
  999.     CMD * VOLATILE oldcurcmd = curcmd;
  1000.     VOLATILE int oldtmps_base = tmps_base;
  1001.     VOLATILE int oldsave = savestack->ary_fill;
  1002.     VOLATILE int oldperldb = perldb;
  1003.     SPAT * VOLATILE oldspat = curspat;
  1004.     SPAT * VOLATILE oldlspat = lastspat;
  1005.     static char *last_eval = Nullch;
  1006.     static long last_elen = 0;
  1007.     static CMD *last_root = Nullcmd;
  1008.     VOLATILE int sp = arglast[0];
  1009.     char *specfilename;
  1010.     char *tmpfilename;
  1011.     int parsing = 1;
  1012.  
  1013.  
  1014.     tmps_base = tmps_max;
  1015.     if (curstash != stash) {
  1016.     (void)savehptr(&curstash);
  1017.     curstash = stash;
  1018.     }
  1019.     str_set(stab_val(stabent("@",TRUE)),"");
  1020.     if (curcmd->c_line == 0)        /* don't debug debugger... */
  1021.     perldb = FALSE;
  1022.     curcmd = &compiling;
  1023.     if (optype == O_EVAL) {        /* normal eval */
  1024.     curcmd->c_filestab = fstab("(eval)");
  1025.     curcmd->c_line = 1;
  1026.     str_sset(linestr,str);
  1027.     str_cat(linestr,";\n");        /* be kind to them */
  1028.     if (perldb)
  1029.         savelines(stab_xarray(curcmd->c_filestab), linestr);
  1030.     }
  1031.     else {
  1032.     if (last_root && !in_eval) {
  1033.         Safefree(last_eval);
  1034.         last_eval = Nullch;
  1035.         cmd_free(last_root);
  1036.         last_root = Nullcmd;
  1037.     }
  1038.     specfilename = str_get(str);
  1039.     str_set(linestr,"");
  1040.     if (optype == O_REQUIRE && &str_undef !=
  1041.       hfetch(stab_hash(incstab), specfilename, strlen(specfilename), 0)) {
  1042.         curcmd = oldcurcmd;
  1043.         tmps_base = oldtmps_base;
  1044.         st[++sp] = &str_yes;
  1045.         perldb = oldperldb;
  1046.         return sp;
  1047.     }
  1048.     tmpfilename = savestr(specfilename);
  1049.     if (index("/.", *tmpfilename))
  1050.         rsfp = fopen(tmpfilename,"r");
  1051.     else {
  1052.         ar = stab_array(incstab);
  1053.         for (i = 0; i <= ar->ary_fill; i++) {
  1054.         (void)sprintf(buf, "%s/%s",
  1055.           str_get(afetch(ar,i,TRUE)), specfilename);
  1056.         rsfp = fopen(buf,"r");
  1057.         if (rsfp) {
  1058.             char *s = buf;
  1059.  
  1060.  
  1061.             if (*s == '.' && s[1] == '/')
  1062.             s += 2;
  1063.             Safefree(tmpfilename);
  1064.             tmpfilename = savestr(s);
  1065.             break;
  1066.         }
  1067.         }
  1068.     }
  1069.     curcmd->c_filestab = fstab(tmpfilename);
  1070.     Safefree(tmpfilename);
  1071.     tmpfilename = Nullch;
  1072.     if (!rsfp) {
  1073.         curcmd = oldcurcmd;
  1074.         tmps_base = oldtmps_base;
  1075.         if (optype == O_REQUIRE) {
  1076.         sprintf(tokenbuf,"Can't locate %s in @INC", specfilename);
  1077.         if (instr(tokenbuf,".h "))
  1078.             strcat(tokenbuf," (change .h to .ph maybe?)");
  1079.         if (instr(tokenbuf,".ph "))
  1080.             strcat(tokenbuf," (did you run h2ph?)");
  1081.         fatal("%s",tokenbuf);
  1082.         }
  1083.         if (gimme != G_ARRAY)
  1084.         st[++sp] = &str_undef;
  1085.         perldb = oldperldb;
  1086.         return sp;
  1087.     }
  1088.     curcmd->c_line = 0;
  1089.     }
  1090.     in_eval++;
  1091.     oldoldbufptr = oldbufptr = bufptr = str_get(linestr);
  1092.     bufend = bufptr + linestr->str_cur;
  1093.     if (++loop_ptr >= loop_max) {
  1094.     loop_max += 128;
  1095.     Renew(loop_stack, loop_max, struct loop);
  1096.     }
  1097.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1098.     loop_stack[loop_ptr].loop_sp = sp;
  1099. #ifdef DEBUGGING
  1100.     if (debug & 4) {
  1101.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1102.     }
  1103. #endif
  1104.     eval_root = Nullcmd;
  1105.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1106.     retval = 1;
  1107.     }
  1108.     else {
  1109.     error_count = 0;
  1110.     if (rsfp) {
  1111.         retval = yyparse();
  1112.         retval |= error_count;
  1113.     }
  1114.     else if (last_root && last_elen == bufend - bufptr
  1115.       && *bufptr == *last_eval && !bcmp(bufptr,last_eval,last_elen)){
  1116.         retval = 0;
  1117.         eval_root = last_root;    /* no point in reparsing */
  1118.     }
  1119.     else if (in_eval == 1 && !savecmd) {
  1120.         if (last_root) {
  1121.         Safefree(last_eval);
  1122.         last_eval = Nullch;
  1123.         cmd_free(last_root);
  1124.         }
  1125.         last_root = Nullcmd;
  1126.         last_elen = bufend - bufptr;
  1127.         last_eval = nsavestr(bufptr, last_elen);
  1128.         retval = yyparse();
  1129.         retval |= error_count;
  1130.         if (!retval)
  1131.         last_root = eval_root;
  1132.         if (!last_root) {
  1133.         Safefree(last_eval);
  1134.         last_eval = Nullch;
  1135.         }
  1136.     }
  1137.     else
  1138.         retval = yyparse();
  1139.     }
  1140.     myroot = eval_root;        /* in case cmd_exec does another eval! */
  1141.  
  1142.  
  1143.     if (retval) {
  1144.     st = stack->ary_array;
  1145.     sp = arglast[0];
  1146.     if (gimme != G_ARRAY)
  1147.         st[++sp] = &str_undef;
  1148.     if (parsing) {
  1149. #ifndef MANGLEDPARSE
  1150. #ifdef DEBUGGING
  1151.         if (debug & 128)
  1152.         fprintf(stderr,"Freeing eval_root %lx\n",(long)eval_root);
  1153. #endif
  1154.         cmd_free(eval_root);
  1155. #endif
  1156.         if ((CMD*)eval_root == last_root)
  1157.         last_root = Nullcmd;
  1158.         eval_root = myroot = Nullcmd;
  1159.     }
  1160.     if (rsfp) {
  1161.         fclose(rsfp);
  1162.         rsfp = 0;
  1163.     }
  1164.     }
  1165.     else {
  1166.     parsing = 0;
  1167.     sp = cmd_exec(eval_root,gimme,sp);
  1168.     st = stack->ary_array;
  1169.     for (i = arglast[0] + 1; i <= sp; i++)
  1170.         st[i] = str_mortal(st[i]);
  1171.                 /* if we don't save result, free zaps it */
  1172.     if (savecmd)
  1173.         eval_root = myroot;
  1174.     else if (in_eval != 1 && myroot != last_root)
  1175.         cmd_free(myroot);
  1176.     }
  1177.  
  1178.  
  1179.     perldb = oldperldb;
  1180.     in_eval--;
  1181. #ifdef DEBUGGING
  1182.     if (debug & 4) {
  1183.     char *tmps = loop_stack[loop_ptr].loop_label;
  1184.     deb("(Popping label #%d %s)\n",loop_ptr,
  1185.         tmps ? tmps : "" );
  1186.     }
  1187. #endif
  1188.     loop_ptr--;
  1189.     tmps_base = oldtmps_base;
  1190.     curspat = oldspat;
  1191.     lastspat = oldlspat;
  1192.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1193.     restorelist(oldsave);
  1194.  
  1195.  
  1196.     if (optype != O_EVAL) {
  1197.     if (retval) {
  1198.         if (optype == O_REQUIRE)
  1199.         fatal("%s", str_get(stab_val(stabent("@",TRUE))));
  1200.     }
  1201.     else {
  1202.         curcmd = oldcurcmd;
  1203.         if (gimme == G_SCALAR ? str_true(st[sp]) : sp > arglast[0]) {
  1204.         (void)hstore(stab_hash(incstab), specfilename,
  1205.           strlen(specfilename), str_smake(stab_val(curcmd->c_filestab)),
  1206.               0 );
  1207.         }
  1208.         else if (optype == O_REQUIRE)
  1209.         fatal("%s did not return a true value", specfilename);
  1210.     }
  1211.     }
  1212.     curcmd = oldcurcmd;
  1213.     return sp;
  1214. }
  1215.  
  1216.  
  1217. int
  1218. do_try(cmd,gimme,arglast)
  1219. CMD *cmd;
  1220. int gimme;
  1221. int *arglast;
  1222. {
  1223.     STR **st = stack->ary_array;
  1224.  
  1225.  
  1226.     CMD * VOLATILE oldcurcmd = curcmd;
  1227.     VOLATILE int oldtmps_base = tmps_base;
  1228.     VOLATILE int oldsave = savestack->ary_fill;
  1229.     SPAT * VOLATILE oldspat = curspat;
  1230.     SPAT * VOLATILE oldlspat = lastspat;
  1231.     VOLATILE int sp = arglast[0];
  1232.  
  1233.  
  1234.     tmps_base = tmps_max;
  1235.     str_set(stab_val(stabent("@",TRUE)),"");
  1236.     in_eval++;
  1237.     if (++loop_ptr >= loop_max) {
  1238.     loop_max += 128;
  1239.     Renew(loop_stack, loop_max, struct loop);
  1240.     }
  1241.     loop_stack[loop_ptr].loop_label = "_EVAL_";
  1242.     loop_stack[loop_ptr].loop_sp = sp;
  1243. #ifdef DEBUGGING
  1244.     if (debug & 4) {
  1245.     deb("(Pushing label #%d _EVAL_)\n", loop_ptr);
  1246.     }
  1247. #endif
  1248.     if (setjmp(loop_stack[loop_ptr].loop_env)) {
  1249.     st = stack->ary_array;
  1250.     sp = arglast[0];
  1251.     if (gimme != G_ARRAY)
  1252.         st[++sp] = &str_undef;
  1253.     }
  1254.     else {
  1255.     sp = cmd_exec(cmd,gimme,sp);
  1256.     st = stack->ary_array;
  1257. /*    for (i = arglast[0] + 1; i <= sp; i++)
  1258.         st[i] = str_mortal(st[i]);  not needed, I think */
  1259.                 /* if we don't save result, free zaps it */
  1260.     }
  1261.  
  1262.  
  1263.     in_eval--;
  1264. #ifdef DEBUGGING
  1265.     if (debug & 4) {
  1266.     char *tmps = loop_stack[loop_ptr].loop_label;
  1267.     deb("(Popping label #%d %s)\n",loop_ptr,
  1268.         tmps ? tmps : "" );
  1269.     }
  1270. #endif
  1271.     loop_ptr--;
  1272.     tmps_base = oldtmps_base;
  1273.     curspat = oldspat;
  1274.     lastspat = oldlspat;
  1275.     curcmd = oldcurcmd;
  1276.     if (savestack->ary_fill > oldsave)    /* let them use local() */
  1277.     restorelist(oldsave);
  1278.  
  1279.  
  1280.     return sp;
  1281. }
  1282.  
  1283.  
  1284. /* This routine handles any switches that can be given during run */
  1285.  
  1286.  
  1287. static char *
  1288. moreswitches(s)
  1289. char *s;
  1290. {
  1291.     int numlen;
  1292.  
  1293.  
  1294.     switch (*s) {
  1295.     case '0':
  1296.     nrschar = scanoct(s, 4, &numlen);
  1297.     nrs = nsavestr("\n",1);
  1298.     *nrs = nrschar;
  1299.     if (nrschar > 0377) {
  1300.         nrslen = 0;
  1301.         nrs = "";
  1302.     }
  1303.     else if (!nrschar && numlen >= 2) {
  1304.         nrslen = 2;
  1305.         nrs = "\n\n";
  1306.         nrschar = '\n';
  1307.     }
  1308.     return s + numlen;
  1309.     case 'a':
  1310.     minus_a = TRUE;
  1311.     s++;
  1312.     return s;
  1313.     case 'c':
  1314.     minus_c = TRUE;
  1315.     s++;
  1316.     return s;
  1317.     case 'd':
  1318. #ifdef TAINT
  1319.     if (euid != uid || egid != gid)
  1320.         fatal("No -d allowed in setuid scripts");
  1321. #endif
  1322.     perldb = TRUE;
  1323.     s++;
  1324.     return s;
  1325.     case 'D':
  1326. #ifdef DEBUGGING
  1327. #ifdef TAINT
  1328.     if (euid != uid || egid != gid)
  1329.         fatal("No -D allowed in setuid scripts");
  1330. #endif
  1331.     debug = atoi(s+1) | 32768;
  1332. #else
  1333.     warn("Recompile perl with -DDEBUGGING to use -D switch\n");
  1334. #endif
  1335.     /*SUPPRESS 530*/
  1336.     for (s++; isDIGIT(*s); s++) ;
  1337.     return s;
  1338.     case 'i':
  1339.     inplace = savestr(s+1);
  1340.     /*SUPPRESS 530*/
  1341.     for (s = inplace; *s && !isSPACE(*s); s++) ;
  1342.     *s = '\0';
  1343.     break;
  1344.     case 'I':
  1345. #ifdef TAINT
  1346.     if (euid != uid || egid != gid)
  1347.         fatal("No -I allowed in setuid scripts");
  1348. #endif
  1349.     if (*++s) {
  1350.         (void)apush(stab_array(incstab),str_make(s,0));
  1351.     }
  1352.     else
  1353.         fatal("No space allowed after -I");
  1354.     break;
  1355.     case 'l':
  1356.     minus_l = TRUE;
  1357.     s++;
  1358.     if (isDIGIT(*s)) {
  1359.         ors = savestr("\n");
  1360.         orslen = 1;
  1361.         *ors = scanoct(s, 3 + (*s == '0'), &numlen);
  1362.         s += numlen;
  1363.     }
  1364.     else {
  1365.         ors = nsavestr(nrs,nrslen);
  1366.         orslen = nrslen;
  1367.     }
  1368.     return s;
  1369.     case 'n':
  1370.     minus_n = TRUE;
  1371.     s++;
  1372.     return s;
  1373.     case 'p':
  1374.     minus_p = TRUE;
  1375.     s++;
  1376.     return s;
  1377.     case 'u':
  1378.     do_undump = TRUE;
  1379.     s++;
  1380.     return s;
  1381.     case 'U':
  1382.     unsafe = TRUE;
  1383.     s++;
  1384.     return s;
  1385.     case 'v':
  1386.     fputs("\nThis is perl, version 4.0\n\n",stdout);
  1387.     fputs(rcsid,stdout);
  1388.     fputs("\nCopyright (c) 1989, 1990, 1991, Larry Wall\n",stdout);
  1389. #ifdef MSDOS
  1390.     fputs("MS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n",
  1391.     stdout);
  1392. #ifdef OS2
  1393.         fputs("OS/2 port Copyright (c) 1990, Raymond Chen, Kai Uwe Rommel\n",
  1394.         stdout);
  1395. #endif
  1396. #endif
  1397.     fputs("\n\
  1398. Perl may be copied only under the terms of either the Artistic License or the\n\
  1399. GNU General Public License, which may be found in the Perl 4.0 source kit.\n",stdout);
  1400. #ifdef MSDOS
  1401.         usage(origargv[0]);
  1402. #endif
  1403.     exit(0);
  1404.     case 'w':
  1405.     dowarn = TRUE;
  1406.     s++;
  1407.     return s;
  1408.     case ' ':
  1409.     case '\n':
  1410.     case '\t':
  1411.     break;
  1412.     default:
  1413.     fatal("Switch meaningless after -x: -%s",s);
  1414.     }
  1415.     return Nullch;
  1416. }
  1417.  
  1418.  
  1419. /* compliments of Tom Christiansen */
  1420.  
  1421.  
  1422. /* unexec() can be found in the Gnu emacs distribution */
  1423.  
  1424.  
  1425. my_unexec()
  1426. {
  1427. #ifdef UNEXEC
  1428.     int    status;
  1429.     extern int etext;
  1430.     static char dumpname[BUFSIZ];
  1431.     static char perlpath[256];
  1432.  
  1433.  
  1434.     sprintf (dumpname, "%s.perldump", origfilename);
  1435.     sprintf (perlpath, "%s/perl", BIN);
  1436.  
  1437.  
  1438.     status = unexec(dumpname, perlpath, &etext, sbrk(0), 0);
  1439.     if (status)
  1440.     fprintf(stderr, "unexec of %s into %s failed!\n", perlpath, dumpname);
  1441.     exit(status);
  1442. #else
  1443. #ifdef MSDOS
  1444.     abort();    /* nothing else to do */
  1445. #else /* ! MSDOS */
  1446. #   ifndef SIGABRT
  1447. #    define SIGABRT SIGILL
  1448. #   endif
  1449. #   ifndef SIGILL
  1450. #    define SIGILL 6        /* blech */
  1451. #   endif
  1452.     kill(getpid(),SIGABRT);    /* for use with undump */
  1453. #endif /* ! MSDOS */
  1454. #endif
  1455. }
  1456.  
  1457.  
  1458.